package com.ly.common;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.*;


public class PageParam<T extends Serializable> implements IPage<T>, Serializable {

    private static final long serialVersionUID = 2405172041950251807L;

    /**
     * 当前数据
     */
    private List<T> records;

    /**
     * 总数
     */
    private long total = 0;

    @TableField( exist = false )
    private Map<String, Object> params;

    /**
     * 当前页数.
     */
    @NotNull(message = "页码数不能为空")
    @Min(value = 1, message = "最小分页为1")
    private long page = 1L;

    /**
     * 每页记录数.
     */
    @Max(value = 500, message = "每页记录数不能超过范围")
    private long limit = 10L;

    /**
     * 排序方式
     */
    @Getter
    @Setter
    private String sortType;

    /**
     * 排序的字段
     */
    @Getter
    @Setter
    private String sortName;

    public Map<String, Object> getParams () {
        if ( params == null ) {
            params = new HashMap<>();
        }
        return params;
    }

    public void setParams ( Map<String, Object> params ) {
        this.params = params;
    }

    @Override
    public List<OrderItem> orders() {
        if (StrUtil.isNotBlank(this.sortName)) {
            List<OrderItem> orderItems;
            String[] sortFields = Arrays.stream(sortName.split(StringPool.COMMA))
                    .map(StrUtil::toUnderlineCase).toArray(String[]::new);
            if ("descend".equalsIgnoreCase(this.sortType)) {
                orderItems = OrderItem.descs(sortFields);
            } else {
                orderItems = OrderItem.ascs(sortFields);
            }
            return orderItems;
        }
        return new ArrayList<>();
    }

    @Override
    public List<T> getRecords() {
        return this.records;
    }

    @Override
    public IPage<T> setRecords(List<T> records) {
        this.records = records;
        return this;
    }

    @Override
    public long getTotal() {
        return this.total;
    }

    @Override
    public IPage<T> setTotal(long total) {
        this.total = total;
        return this;
    }

//    @JsonIgnore
    @Override
    public IPage<T> setCurrent(long current) {
        this.page = current;
        return this;
    }

//    @JsonIgnore
    @Override
    public long getSize() {
        return limit;
    }

    @Override
    public IPage<T> setSize(long size) {
        this.limit = size;
        return this;
    }

    @Override
    public long getCurrent() {
        return this.page;
    }

    // 新增别名方法
    @JsonIgnore
    public long getLimit() {
        return getSize();
    }

    @JsonIgnore
    public IPage<T> setLimit(long limit) {
        return setSize(limit);
    }
    @JsonIgnore
    public long getPage() {
        return getCurrent();
    }

    public IPage<T> setPage(long page) {
        return setCurrent(page);
    }

}